package com.moyun.zbmy.zizhou.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.beans.HighRiseBean;
import com.moyun.zbmy.zizhou.beans.HighRiseCharacterBean;
import com.moyun.zbmy.zizhou.beans.HighRiseImage;
import com.moyun.zbmy.zizhou.beans.HighRiseNewsInfo;
import com.moyun.zbmy.zizhou.beans.HighRiseTypeBean;
import com.moyun.zbmy.zizhou.beans.HighRiseUser;
import com.moyun.zbmy.zizhou.beans.NewsItem;
import com.moyun.zbmy.zizhou.http.AbsListNetCallback;
import com.moyun.zbmy.zizhou.http.AbsNetCallBack;
import com.moyun.zbmy.zizhou.http.NetUtils;
import com.moyun.zbmy.zizhou.ui.adapter.HighRiseAdapter;
import com.moyun.zbmy.zizhou.ui.adapter.HighRiseNewsListAdapter;
import com.moyun.zbmy.zizhou.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HighRiseSubFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {

    @BindView(R.id.fm_high_rise_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;

    private int userSize = 0;
    private HighRiseAdapter adapter;
    private int refreshCount = 0;

    private List<IListShowData> highRiseList = new ArrayList<>();

    public HighRiseSubFragment() {
        // Required empty public constructor
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sub_high_rise, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onRefresh() {
        refreshCount++;
        if (refreshCount > 1 && getParentFragment() instanceof HighRiseFragment) {
            HighRiseFragment parent = (HighRiseFragment) getParentFragment();
            parent.refreshData();
        }
        refreshLayout.refreshComplete();
    }

    @Override
    public void onLoadMore() {
        refreshLayout.loadMoreComplete();
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(false);
//        refreshLayout.setRefreshEnabled(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new HighRiseAdapter(getContext(), this);
        recyclerView.setAdapter(adapter);
        refreshLayout.autoRefresh();
    }


    @Override
    public void onResume() {
        super.onResume();
        JLog.e("--------------HighRiseFragment-----refreshData---onResume-----------");
        adapter.setDataList(highRiseList);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }


    @Override
    public void onPause() {
        super.onPause();

    }


    public void setList(List<IListShowData> list) {
        if (ListUtils.isListValued(list)) {
            highRiseList.addAll(list);
        }
    }

    public void updateList(List<IListShowData> list) {
        if (ListUtils.isListValued(list)) {
            highRiseList.clear();
            highRiseList.addAll(list);
            adapter.setDataList(highRiseList);
        }
    }

    OnItemInternalClick onItemSubscribeClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {

        }
    };

}
